import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_douyin/flutter_douyin.dart';
import 'package:flutter_douyin/flutter_douyin_auth.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _dyClentKey_sandbox = "awcy5uz4jc4zqx88"; //沙箱环境

  late String _auth = "授权中...";

  String? version = "";

  String? _path;

  @override
  void initState() {
    super.initState();
    initData();
  }

  initData() async {
    version = await FlutterDouyin.platformVersion;
    await FlutterDouYinAuth.initPlatform(
        _dyClentKey_sandbox, _dyClentKey_sandbox);
  }

  void auth() {
    FlutterDouYinAuth.initPlatform(_dyClentKey_sandbox, _dyClentKey_sandbox);

    List<String> permissionMest = ["fans.list", "user_info"];
    List<String> permissionOptional = [];

    permissionOptional.add("fans.data");

    FlutterDouYinAuth.douyinOpenSDKAuthRequest(
        permissionMest, permissionOptional, success: (result) {
      print("授权返回:$result");
    }, fail: (code, message) {
      print("授权返回失败:code=" + code.toString() + "  message=" + message);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Center(
              child: Text('$version'),
            ),
            Center(
              child: Text('授权信息: $_auth'),
            ),
            Container(
              child: TextButton(
                  onPressed: () {
                    print("用户请求授权");
                    // douyinOpenSDKAuthRequest();
                    auth();
                  },
                  child: Text("授权")),
            ),
            Container(
              child: TextButton(
                  onPressed: () {

                  },
                  child: Text("分享")),
            ),
          ],
        ),
      ),
    );
  }
}
